<?php

include_once 'database.php';
include_once 'supervisor.php';
include_once 'subject.php';

class Site {
    
    public static function getSiteBasics() {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from general where property = 'std_sup_ratio'");
        $row = mysql_fetch_assoc($result);
        $std_sup_ratio = $row['value'];
        
        $result = $dbo->mysql_query("select * from general where property = 'sup_exam_mincount'");
        $row = mysql_fetch_assoc($result);
        $sup_exam_mincount  = $row['value'];
        
        $basics = array('std_sup_ratio' => $std_sup_ratio, 'sup_exam_mincount'=>$sup_exam_mincount );
        
        return $basics;
    }
    
    public static function setSiteBasics($std_sup_ratio, $sup_exam_mincount) {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from general where property = 'std_sup_ratio'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'std_sup_ratio'");
        $result = $dbo->mysql_query("insert into general values ('std_sup_ratio', '$std_sup_ratio')");
        
        $result = $dbo->mysql_query("select * from general where property = 'sup_exam_mincount'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'sup_exam_mincount'");
        $result = $dbo->mysql_query("insert into general values ('sup_exam_mincount', '$sup_exam_mincount')");
    }
    
    /**
     * This will returnn an asoociative array with with the details about the email 
     * settings required
     * 
     * @return String $detail['email'] Email address
     * @return String $detail['smtp'] SMTP address
     * @return String $detail['password'] Password of the email account
     */ 
    public static function getMailDetail() {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from general where property = 'mail_email'");
        $row = mysql_fetch_assoc($result);
        $mail_email = $row['value'];
        
        $result = $dbo->mysql_query("select * from general where property = 'mail_password'");
        $row = mysql_fetch_assoc($result);
        $mail_password = $row['value'];
        
        $result = $dbo->mysql_query("select * from general where property = 'mail_smtp'");
        $row = mysql_fetch_assoc($result);
        $mail_smtp = $row['value'];
        
        $detail = array('email' => $mail_email, 'password' => $mail_password, 'smtp' => $mail_smtp);
        
        return $detail;
    }
    
    public static function setMailDetail($email , $password, $smtp) {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from general where property = 'mail_email'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mail_email'");
        $result = $dbo->mysql_query("insert into general values ('mail_email', '$email')");
        
        $result = $dbo->mysql_query("select * from general where property = 'mail_password'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mail_password'");
        $result = $dbo->mysql_query("insert into general values ('mail_password', '$password')");
        
        $result = $dbo->mysql_query("select * from general where property = 'mail_email'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mail_smtp'");
        $result = $dbo->mysql_query("insert into general values ('mail_smtp', '$smtp')");
    }
    
    
    public static function getMailSendingOptions() {
        $dbo = Database::getDBOInstance();
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_signup'");
        $row = mysql_fetch_assoc($result);
        $mso_on_signup = $row['value'];
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_enrolment'");
        $row = mysql_fetch_assoc($result);
        $mso_on_enrolment = $row['value'];
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_drop'");
        $row = mysql_fetch_assoc($result);
        $mso_on_drop = $row['value'];
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_complete'");
        $row = mysql_fetch_assoc($result);
        $mso_on_complete = $row['value'];
        
        $details = array('mso_on_signup' => $mso_on_signup, 'mso_on_enrolment' => $mso_on_signup, 'mso_on_drop' =>$mso_on_drop, 'mso_on_complete'=>$mso_on_complete);
        return $details;    
    }
    
    public static function setMailSendingOptions($chk_on_signup, $chk_on_enrolment, $chk_on_drop, $chk_on_complete) {
        $dbo = Database::getDBOInstance();
        
        $chk_on_signup = $chk_on_signup ? 1 : 0;
        $chk_on_enrolment = $chk_on_enrolment ? 1 : 0;
        $chk_on_drop = $chk_on_drop ? 1 : 0;
        $chk_on_complete = $chk_on_complete ? 1 : 0;
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_signup'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mso_on_signup'");
        $result = $dbo->mysql_query("insert into general values ('mso_on_signup', '$chk_on_signup')");
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_signup'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mso_on_enrolment'");
        $result = $dbo->mysql_query("insert into general values ('mso_on_enrolment', '$chk_on_enrolment')");
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_drop'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mso_on_drop'");
        $result = $dbo->mysql_query("insert into general values ('mso_on_drop', '$chk_on_drop')");
        
        $result = $dbo->mysql_query("select * from general where property = 'mso_on_complete'");
        if (mysql_num_rows($result) > 0)
            $result = $dbo->mysql_query("delete from general where property = 'mso_on_complete'");
        $result = $dbo->mysql_query("insert into general values ('mso_on_complete', '$chk_on_complete')");
    }
    
    
}

?>